Conditions | 9 |
Paths | 2 |
Total Lines | 116 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** global: chrome */ |
||
76 | function appsToDomTemplate(response) { |
||
77 | let template = []; |
||
78 | |||
79 | if ( response.tabCache && Object.keys(response.tabCache.detected).length > 0 ) { |
||
80 | const categories = {}; |
||
81 | |||
82 | // Group apps by category |
||
83 | for ( let appName in response.tabCache.detected ) { |
||
84 | response.apps[appName].cats.forEach(cat => { |
||
85 | categories[cat] = categories[cat] || { apps: [] }; |
||
86 | |||
87 | categories[cat].apps[appName] = appName; |
||
88 | }); |
||
89 | } |
||
90 | |||
91 | for ( let cat in categories ) { |
||
92 | const apps = []; |
||
93 | |||
94 | for ( let appName in categories[cat].apps ) { |
||
95 | let confidence = response.tabCache.detected[appName].confidenceTotal; |
||
96 | let version = response.tabCache.detected[appName].version; |
||
97 | |||
98 | apps.push( |
||
99 | [ |
||
100 | 'a', { |
||
101 | class: 'detected__app', |
||
102 | target: '_blank', |
||
103 | href: 'https://www.wappalyzer.com/technologies/' + slugify(appName) |
||
104 | }, [ |
||
105 | 'img', { |
||
106 | class: 'detected__app-icon', |
||
107 | src: '../images/icons/' + ( response.apps[appName].icon || 'default.svg' ) |
||
108 | }, |
||
109 | ], [ |
||
110 | 'span', { |
||
111 | class: 'detected__app-name' |
||
112 | }, |
||
113 | appName, |
||
114 | ], version ? [ |
||
115 | 'span', { |
||
116 | class: 'detected__app-version' |
||
117 | }, |
||
118 | version |
||
119 | ] : null, confidence < 100 ? [ |
||
120 | 'span', { |
||
121 | class: 'detected__app-confidence' |
||
122 | }, |
||
123 | confidence + '% sure' |
||
124 | ] : null |
||
125 | ] |
||
126 | ); |
||
127 | } |
||
128 | |||
129 | template.push( |
||
130 | [ |
||
131 | 'div', { |
||
132 | class: 'detected__category' |
||
133 | }, [ |
||
134 | 'div', { |
||
135 | class: 'detected__category-name' |
||
136 | }, [ |
||
137 | 'a', { |
||
138 | class: 'detected__category-link', |
||
139 | target: '_blank', |
||
140 | href: 'https://www.wappalyzer.com/categories/' + slugify(response.categories[cat].name) |
||
141 | }, |
||
142 | browser.i18n.getMessage('categoryName' + cat), |
||
143 | ], [ |
||
144 | 'span', { |
||
145 | class: 'detected__category-pin-wrapper' + ( pinnedCategory == cat ? ' detected__category-pin-wrapper--active' : '' ), |
||
146 | 'data-category-id': cat, |
||
147 | 'title': browser.i18n.getMessage('categoryPin'), |
||
148 | }, [ |
||
149 | 'img', { |
||
150 | class: 'detected__category-pin detected__category-pin--active', |
||
151 | src: '../images/pin-active.svg' |
||
152 | }, |
||
153 | ], [ |
||
154 | 'img', { |
||
155 | class: 'detected__category-pin detected__category-pin--inactive', |
||
156 | src: '../images/pin.svg' |
||
157 | } |
||
158 | ] |
||
159 | ] |
||
160 | ], [ |
||
161 | 'div', { |
||
162 | class: 'detected__apps' |
||
163 | }, |
||
164 | apps |
||
165 | ] |
||
166 | ] |
||
167 | ); |
||
168 | } |
||
169 | |||
170 | template = [ |
||
171 | 'div', { |
||
172 | class: 'detected' |
||
173 | }, |
||
174 | template |
||
175 | ]; |
||
176 | } else { |
||
177 | template = [ |
||
178 | 'div', { |
||
179 | class: 'empty' |
||
180 | }, |
||
181 | [ |
||
182 | 'span', { |
||
183 | class: 'empty__text' |
||
184 | }, |
||
185 | browser.i18n.getMessage('noAppsDetected') |
||
186 | ], |
||
187 | ]; |
||
188 | } |
||
189 | |||
190 | return template; |
||
191 | } |
||
192 | |||
196 |